﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
using MvcEngine.Core.Repositories;

using System.Data.Linq;
using MvcEngine.Core.Content;
using MvcEngine.Core.Helpers;

namespace MvcEngine.LinqToSqlProvider
{
    public class PageContentRepository : Repository, IPageContentRepository
    {
        public IList<PageContent> Get(Guid pageId, Guid controlId)
        {
            using (var contextManager = CreateContextManager(true))
            {
                return contextManager.Context.PageContents().Where(p => p.PageId == pageId && p.ControlId == controlId).ToList();
            }
        }

        public void DeleteByModule(int moduleId)
        {
            throw new NotImplementedException();
            //context.GetTable<PageContent>().DeleteAllOnSubmit(context.GetTable<PageContent>().Where(m => m.ControlId == moduleId));
            //context.SubmitChanges();
        }

        public IList<PageContent> GetByControl(Guid controlId, Guid siteId)
        {
            using (var contextManager = CreateContextManager(true))
            {
                return contextManager.Context.PageContents().Where(p => p.ControlId == controlId && p.SiteId == siteId).ToList();
            }
        }

        public IList<PageContent> GetByPage(Guid pageId)
        {
            using (var contextManager = CreateContextManager(true))
            {
                return contextManager.Context.PageContents().Where(p => p.PageId == pageId).ToList();
            }
        }

        public IList<PageContentDetails> GetPageContentDetailsByPage(Guid pageId)
        {
            using (var contextManager = CreateContextManager(true))
            {
                var res = from pc in contextManager.Context.PageContents()
                          join
                              control in contextManager.Context.Controls() on pc.ControlId equals control.Id
                          join
                              container in contextManager.Context.Containers() on pc.ContainerId equals container.Id
                          orderby pc.Position
                          where pc.PageId == pageId
                          select new PageContentDetails()
                          {
                              ContainerId = container.Id,
                              ContainerMarkupId = container.MarkupId,
                              ContentName = pc.ContentName,
                              ControlId = control.Id,
                              ControlName = control.Name,
                              Id = pc.Id,
                              MaxModuleCount = container.MaxModuleCount,
                              PageId = pc.PageId,
                              Position = pc.Position,
                              SiteId = pc.SiteId,
                              ReferenceId = pc.ReferenceId
                          };
                return res.ToList();
            }
        }

        public IList<Module> GetPageModules(Guid pageId)
        {
            throw new Exception("Must be changed");
            //var res = from c in context.GetTable<PageContent>()
            //              where c.PageId == pageId
            //              select c.Module;

            //    return new List<IModule>(res.Cast<IModule>());
        }

        public void Save(PageContent pageContent)
        {
            using (var contextManager = CreateContextManager(false))
            {
                var oldItem = Get(contextManager.Context, pageContent.Id);
                if (oldItem != null)
                {
                    oldItem.ModifiedDate = DateTime.Now;
                    oldItem.ContainerId = pageContent.ContainerId;
                    oldItem.ContentName = pageContent.ContentName;
                    oldItem.Position = pageContent.Position;
                    oldItem.Xml = pageContent.Fields;
                }
                else
                {
                    pageContent.Xml = pageContent.Fields;
                    contextManager.Context.PageContents().InsertOnSubmit(pageContent);
                }
                contextManager.Commit();
            }
        }

        public void DeleteByModule(Guid moduleId)
        {
            throw new NotImplementedException();
        }

        public IList<PageContent> GetAll()
        {
            throw new NotImplementedException();
        }

        public void Delete(int id)
        {
            using (var contextManager = CreateContextManager(false))
            {
                contextManager.Context.PageContents().DeleteOnSubmit(contextManager.Context.PageContents().Single(p => p.Id == id)); 
                contextManager.Commit();
            }
        }

        public void DeleteAll()
        {
            throw new NotImplementedException();
        }

        public PageContent Get(int id)
        {
            using (var contextManager = CreateContextManager(true))
            {
                return Get(contextManager.Context, id);
            }
        }

        private PageContent Get(DataContext context, int id)
        {
            return context.PageContents().SingleOrDefault(p => p.Id == id);
        }

    }
}
